<?php

namespace app\common\validate\xilujob;

use app\common\model\xilujob\Benefit;
use app\common\model\xilujob\EducationLevel;
use think\Validate;
use function fast\array_get;

/**
 * 不适合作为模型验证器, 包含job表不具有的字段， 应作为参数验证器
 * Class Job
 * @package app\common\validate
 */
class Job extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
        //table: job
        'publisher_id' => ['require'],
        'publisher_type' => ['require','in:2,3'], //仅能是公司、店铺
        'type|职位类型' => ['require','in:1,2'],
        'job_name|职位名称' => ['require','length:1,20'],
        'job_category_id|职位分类' => ['require','category_exist'],
        'city_id|发布城市' => ['require','area_exist'],
        'district_id|发布区域' => ['require','area_exist'],
        'work_experience_id|工作经验' => ['require','work_experience_exist'],
        'education_level_id|学历要求' => ['require','education_exist'],
        'salary_type|薪资类型' => ['require', 'in:0,1,2,3','salary'],
        'salary_min|最低薪资' => ['integer','egt:0'],
        'salary_max|最高薪资' => ['integer','egt:0'],
        'in_hurry' => ['in:0,1'],
        'direct' => ['in:0,1'],
        'shop' => ['in:0,1'],
        'red_packet|补贴金额' => ['integer','egt:0'],
        'red_packet_instruction|补贴说明' => ['length:0,200'],
        'num|招聘人数' => ['integer', 'gt:0'],

        //table: job_info
        'responsibility|职位描述' => ['require', 'length:1,500'],
//        'requirement|任职要求' => ['require', 'length:1,500'],
        'environment_images|工作环境照片' => ['requireIf:type,3'],
        'contact_person|联系人' => ['require','length:1,20'],
        'contact_phone|联系电话' => ['require','length:1,20'],
        'contact_email|联系邮箱' => ['email','length:1,40'],

        //table: job_benefit_relation
        'benefit_ids' => ['benefits_valid'],
        //table job_location
        'locations' => ['require', 'array', 'length:1,5', 'locations_valid']
    ];
    /**
     * 提示消息
     */
    protected $message = [
        'locations' => '至少一个，最多5个工作地点'
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => ['publisher_id','publisher_type','type', 'job_name', 'job_category_id', 'city_id','district_id','responsibility',
            'environment_images','contact_person','contact_phone','contact_email','work_experience_id',
            'education_level_id','salary_type','salary_min','salary_max','num','in_hurry','direct','shop','red_packet',
            'red_packet_instruction','locations','benefit_ids'],

        'edit' => ['type', 'job_name', 'job_category_id', 'city_id','district_id','responsibility',
            'environment_images','contact_person','contact_phone','contact_email','work_experience_id',
            'education_level_id','salary_type','salary_min','salary_max','num','in_hurry','direct','shop','red_packet',
            'red_packet_instruction','locations','benefit_ids'],
    ];

    protected function category_exist($value, $rule, $data) {
        return \app\admin\model\xilujob\JobCategory::where('id',$value)->count()>0;
    }

    protected function area_exist($value, $rule, $data) {
        return \app\admin\model\xilujob\Area::where('id',$value)->count()>0 ? true:'城市区域属性不正确';
    }

    protected function salary($value, $rule, $data) {
        if($data['salary_type']==0) {
            return true;
        }
        $salaryMin = array_get($data,'salary_min');
        $salaryMax = array_get($data,'salary_max');
        if(!$salaryMin && !$salaryMax) {
            return '最低薪资和最高薪资至少填一项';
        }
        return true;
    }

    protected function work_experience_exist($value, $rule, $data) {
        return \app\admin\model\xilujob\WorkExperience::where('id',$value)->count()>0;
    }

    protected function education_exist($value, $rule, $data) {
        return EducationLevel::where('id',$value)->count()>0;
    }

    protected function benefits_valid($value, $rule, $data) {
        if(!$value) {
            return true;
        }
        $ids = is_array($value) ? $value : explode(',', $value);
        $count = Benefit::whereIn('id', $ids)->count();
        if(count($ids)!==$count) {
            return '福利选择错误';
        }
        return true;
    }

    protected function locations_valid($value, $rule, $data) {
        foreach($value as $location) {
            $validator = Validate::make([
                'province_id' => ['require'], 'city_id'=>['require'], 'district_id'=> ['require'], 'address'=>['require','length:1,40']
            ]);
            if(!$validator->check($location)) {
                return '工作地点错误';
            }
        }
        return true;
    }

}
